import os
import secrets
import warnings
from typing import Annotated, Any, Literal

from pydantic import (
    AnyUrl,
    BeforeValidator,
    EmailStr,
    HttpUrl,
    PostgresDsn,
    MySQLDsn,
    computed_field,
    model_validator,
)
from pydantic_core import MultiHostUrl
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    # model_config = SettingsConfigDict(
    #     # Use top level .env file (one level above ./backend/)
    #     env_file="../.env",
    #     env_ignore_empty=True,
    #     extra="ignore",
    # )
    API_STR: str = "/api"
    # 静态资源目录
    STATIC_DIR: str = os.path.join(os.getcwd(), "static")
    TEMPLATE_DIR: str = os.path.join(STATIC_DIR, "templates")
    # 上传路径URL前缀
    upload_prefix: str = '/static/uploads'
    # 上传文件路径
    UPLOAD_DIR: str = os.path.join(STATIC_DIR, "uploads")
    # JWT 加密密钥
    SECRET_KEY: str = secrets.token_urlsafe(32)
    # 60 minutes * 24 hours * 8 days = 8 days
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
    FRONTEND_HOST: str = "http://localhost:5173"
    ENVIRONMENT: Literal["local", "staging", "production"] = "local"

    # 时区
    TIMEZONE: str = 'Asia/Shanghai'
    # 日期时间格式
    DATETIME_FORMAT: str = '%Y-%m-%d %H:%M:%S'

    PROJECT_NAME: str = "FastAPI-Template"
    SENTRY_DSN: HttpUrl | None = None
    DB_HOST: str = "localhost"
    DB_PORT: int = 3306
    DB_USER: str = "root"
    DB_PASSWORD: str = ""
    DB_DB: str = "fastapi_cms"

    @computed_field  # type: ignore[prop-decorator]
    @property
    def SQLALCHEMY_DATABASE_URI(self) -> str:
        return f"postgresql+psycopg://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_DB}"


settings = Settings()  # type: ignore
